#include <GL/gl.h>
#include <GL/glu.h>
#include <SDL/SDL.h>
#include <iostream>
#include "Particles.h"

#define window_width  640
#define window_height 480
// Keydown booleans
bool key[321];

// Process pending events
bool events()
{
	SDL_Event event;
	if (SDL_PollEvent(&event) )
	{
		switch (event.type)
		{
		case SDL_KEYDOWN:
			key[ event.key.keysym.sym ]=true;
			break;
		case SDL_KEYUP:
			key[ event.key.keysym.sym ]=false;
			break;
		case SDL_QUIT:
			return false;
			break;
		}
	}
	return true;
}

void mainLoopFunction()
{
	Particles particles;
	float angle=0;

	Uint32 startTime = SDL_GetTicks();
	Uint32 frames = 0;

	while (events() )
	{
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glLoadIdentity();
		glTranslatef(0, 0, -20);
		glRotatef(angle, 0, 0, 1);
		glTranslatef(-5, -5, 0);
		glPointSize(5);

		glBegin(GL_LINES);
		glColor3ub(255, 000, 000);
		glVertex2f( 00, 10);
		glColor3ub(255, 000, 000);
		glVertex2f( 10, 10);

		glColor3ub(255, 255, 000);
		glVertex2f( 10, 10);
		glColor3ub(255, 255, 000);
		glVertex2f( 10, 0);

		glColor3ub(000, 255, 000);
		glVertex2f( 10, 00);
		glColor3ub(000, 255, 000);
		glVertex2f( 00, 00);

		glColor3ub(000, 000, 255);
		glVertex2f( 00, 00);
		glColor3ub(000, 000, 255);
		glVertex2f( 00, 10);
		glEnd();

		particles.nextFrame();

		glBegin(GL_POINTS);

		int value = 0;
		for (int i = 0; i < Particles::NUM_PARTICLES; i++)
		{
//			value = particles.velY[i] * particles.velY[i] * 255.0 / 100.0;
			value = 128 + particles.velY[i] * 255.0 / 10.0 / 2;
			if (value < 0)
				value = 0;
			if (value > 255)
				value = 255;
			glColor3ub(value, 255-value, 000);
			glVertex2f(particles.posX[i], particles.posY[i]);
		}

		glEnd();
		SDL_GL_SwapBuffers();
		// Check keypresses
		if (key[SDLK_RIGHT])
		{
			angle-=0.05;
		}
		if (key[SDLK_LEFT ])
		{
			angle+=0.05;
		}

		++frames;
	}

	Uint32 totalTime = SDL_GetTicks() - startTime;
	Uint32 fps = frames / (totalTime / 1000);
	std::cout << "Total time: " << totalTime << "ms. Frames: " << frames
			<< ". FPS:" << fps << std::endl;
}

// Initialze OpenGL perspective matrix
void GL_Setup(int width, int height)
{
	glViewport( 0, 0, width, height);
	glMatrixMode( GL_PROJECTION);
	glEnable( GL_DEPTH_TEST);
	gluPerspective( 45, (float)width/height, 0.1, 100);
	glMatrixMode( GL_MODELVIEW);
}

int main(int argc, char **argv)
{
	using namespace std;

	// Initialize SDL with best video mode
	if (SDL_Init(SDL_INIT_VIDEO) != 0)
	{
		// Failure
		cerr << "Error: " << SDL_GetError() << endl;
		return EXIT_FAILURE;
	}
	// Success
	cout << "SDL initialized." << endl;

	const SDL_VideoInfo* info = SDL_GetVideoInfo();
	int vidFlags= SDL_OPENGL | SDL_GL_DOUBLEBUFFER;
	if (info->hw_available)
	{
		vidFlags |= SDL_HWSURFACE;
	}
	else
	{
		vidFlags |= SDL_SWSURFACE;
	}
	int bpp = info->vfmt->BitsPerPixel;
	//SDL_Surface *screen =
			SDL_SetVideoMode(window_width, window_height, bpp, vidFlags);
	GL_Setup(window_width, window_height);
	mainLoopFunction();

	return EXIT_SUCCESS;
}
